
var alumnus = angular.module('alumnus', ['ngResult']);

function findController($scope , $filter) {
	$scope.alumnus = [];
	for (var i =1 ; i < 100 ; i++) {
		$scope.alumnus.push({
			id : i ,
			name : "name-" + i ,
			phone : "138999" + i , 
			company : "e-factory - " + i ,
			city : "shanghai - " + i 
		});
	};

	$scope.result = {
		data : $scope.alumnus ,
		columns : [ 
			{ key: "id" , name : "ID"}  ,
			{ key: "name" , name : "Name"}  ,
			{ key: "phone" , name : "Phone"}  ,
			{ key: "company" , name : "Company"}  ,
			{ key: "city" , name : "City"} 
		] ,
		nofilter : true 
	}
	$scope.$watch('{ id: id , name : name}', function(conditions){
		console.log(conditions);
		var list = $filter('filter')( $scope.alumnus , conditions );
		$scope.result.draw(list);
	} , true);

}

function fuzzyFindController($scope) {
	$scope.alumnus = [];
	for (var i =1 ; i < 100 ; i++) {
		$scope.alumnus.push({
			name : "name - " + i ,
			id : i ,
			city : "shanghai - " + i , 
			phone : "138999" + i , 
			company : "e-factory - " + i 
		});
	};
}

function categoryFindController($scope){
	$scope.alumnus = [];
	for (var i =1 ; i < 100 ; i++) {
		$scope.alumnus.push({
			name : "name - " + i ,
			id : i ,
			city : "shanghai - " + i , 
			phone : "138999" + i , 
			company : "e-factory - " + i 
		});
	};

	$scope.categories = {
		college : [{
				name : "哲学学院" ,
				click :  "setFaculty" ,
				list: [{
						name: "古典哲学" ,
						list: [{ name: "G1班" } , { name: "G2班" }, { name: "G3班" }] 
					} , {
						name: "现代哲学" ,
						list: [{ name: "H1班" } , { name: "H2班" }, { name: "H3班" }] 
					}] 
			} , {
				name : "信息学院" ,
				click :  "setFaculty" ,
				list: [{
						name: "电子工程系" ,
						list: [{ name: "T1班" } , { name: "T2班" }, { name: "T3班" }] 
					} , {
						name: "光科学与工程系" ,
						list: [{ name: "H1班" } , { name: "H2班" }, { name: "H3班" }] 
					} , {
						name: "通信科学与工程系" ,
						list: [{ name: "H1班" } , { name: "H2班" }, { name: "H3班" }] 
					} ] 
			} , {
				name : "经济学院" ,
				click :  "setFaculty" ,
				list: [{
						name: "宏观经济" ,
						list: [{ name: "H1班" } , { name: "H2班" }, { name: "H3班" }] 
					} , {
						name: "微观经济" ,
						list: [{ name: "W1班" } , { name: "W2班" }, { name: "W3班" }] ,
					}] 
			}] ,
		students: [{
				name: "校友总会1" ,
				list: [{ name: "上海分会" }  , { name: "北京分会" } ]  
			} , {
				name: "校友总会2" ,
				list: [{ name: "上海分会" }  , { name: "深圳分会" } ]  
			} ] ,
		age : [{ name: "1960" } , { name: "1970" }, { name: "1980" }, { name: "1990" }] ,
		faculty : [] ,
		classes : [] ,
		branch : [] 
	};

	$scope.setFaculty = function( college){
		$scope.categories.faculty = college.list;
	}
	$scope.setClasses = function(faculty){
		$scope.categories.classes = faculty.list;
	}
	$scope.setBranch = function(students){
		$scope.categories.branch = students.list;
	}

}

function findIndexController($scope , $filter) {
	$scope.alumnus = [];
	$scope.result = {
		data : $scope.alumnus ,
		columns : [ 
			{ key: "id" , name : "ID"}  ,
			{ key: "name" , name : "姓名"}  ,
			{ key: "grader" , name : "性别"}  ,
			{ key: "live" , name : "在世"}  ,
			{ key: "state" , name : "认证"} 
		] ,
		nofilter : true 
	}
	$scope.$watch('{ id: id , name : name}', function(conditions){
		var list = $filter('filter')( $scope.alumnus , conditions );
		$scope.result.draw(list);
	} , true);


	$scope.filtered = {};

	$scope.filterClick = function(key , value ){
		if( key ) {
			$scope.filtered[key] = value;
		}
	}


	$scope.$watch('filtered', function(values){

		console.log(values);
		var list = $scope.alumnus;
		angular.forEach(values , function( k){
			list = $filter('filter')(list , k);
		});

		// var list = $filter('filter')( $scope.alumnus , values);
		$scope.result.draw(list);
	} , true);


	$scope.$watch('query', function(query){

		if( !query) {
			return;
		}

		var alumnus = [];
		for (var i =1 ; i < 100 ; i++) {
			var c  = i % 2 == 0;
			alumnus.push({
				id : i ,
				name : "name-" + i ,
				grader : c ? "women" : "man" , 
				live : c ? "否"  : "是" ,
				state :  c ? "已认证"  : "未认证" 
			});
		}

		var list = $filter('filter')( alumnus , query);

		$scope.alumnus = list; 

		$scope.result.draw(list);
	} , true);
}



var Combination = Class.extend({
	init : function(fields){
		this.fields = fields || []; 
	}
});

var CombinationFactory = Class.extend({
	init : function( comb ) {
		this.instances = [];
		this.instances.push(comb);
	} ,
	add : function(comb ){
		this.instances.push(comb);
	}
});
 

function combinationFindIndexController($scope , $filter) {

	var fileds = [
		{ name : '' , title : '姓名' } ,
		{ name : '' , title : '曾用名' } ,
		{ name : '' , title : '籍贯' } ,
		{ name : '' , title : '民族' } ,
		{ name : '' , title : '政治面貌' } ,
		{ name : '' , title : '所在地' } ,
		{ name : '' , title : '学号' } ,
		{ name : '' , title : '入学年份' } ,
		{ name : '' , title : '毕业年份' } ,
		{ name : '' , title : '院系' } ,
		{ name : '' , title : '专业' } ,
		{ name : '' , title : '班级' } ,
		{ name : '' , title : '学历' } ,
		{ name : '' , title : '学位' } ,
		{ name : '' , title : '学制' } ,
		{ name : '' , title : '培养方式' } ,
		{ name : '' , title : '单位名称' } ,
		{ name : '' , title : '单位性质' } ,
		{ name : '' , title : '单位行业' } ,
		{ name : '' , title : '职务名称' } ,
		{ name : '' , title : '所属校友会' } ,
		{ name : '' , title : '校友会职务' }
	];

	$scope.comb =  new CombinationFactory(new Combination(fileds));

	$scope.alumnus = [];
	$scope.result = {
		data : $scope.alumnus ,
		columns : [ 
			{ key: "id" , name : "ID"}  ,
			{ key: "name" , name : "姓名"}  ,
			{ key: "grader" , name : "性别"}  ,
			{ key: "live" , name : "在世"}  ,
			{ key: "state" , name : "认证"} 
		] ,
		nofilter : true 
	}
	$scope.$watch('{ id: id , name : name}', function(conditions){
		var list = $filter('filter')( $scope.alumnus , conditions );
		$scope.result.draw(list);
	} , true);

	$scope.filtered = {};

	$scope.filterClick = function(key , value ){
		if( key ) {
			$scope.filtered[key] = value;
		}
	}


	$scope.$watch('filtered', function(values){
		var list = $scope.alumnus;
		angular.forEach(values , function( k){
			list = $filter('filter')(list , k);
		});
		$scope.result.draw(list);
	} , true);


	$scope.$watch('query', function(query){

		if( !query) {
			return;
		}

		var alumnus = [];
		for (var i =1 ; i < 100 ; i++) {
			var c  = i % 2 == 0;
			alumnus.push({
				id : i ,
				name : "name-" + i ,
				grader : c ? "women" : "man" , 
				live : c ? "否"  : "是" ,
				state :  c ? "已认证"  : "未认证" 
			});
		}

		var list = $filter('filter')( alumnus , query);

		$scope.alumnus = list; 

		$scope.result.draw(list);
	} , true);



}

